package com.ms;

/**
 * ==  equals 的区别？
 * 1）== 比较的是内存地址， equals比较的内容（未必）
 * 2）Object 的 equals  比较的还是内存地址
 * 3) String 的 equals  方法先比较 内存地址，然后比较内容
 * @author carter
 * create time    2018-08-20
 */
public class TestQuestton1 {

    public static void main(String[] args) {

        String str = new String("abc");
        String str1 = new String("abc");

        System.out.println(str == str1);//false
        System.out.println(str.equals( str1)); //true


        Object obj1= new Object();
        Object obj2= new Object();

        System.out.println(obj1 == obj2);//false
        System.out.println(obj1.equals(obj2));//false return (this == obj); 比较的还是内存地址
        System.out.println(obj1.equals(obj1));//true


        Person zhangsan = new Person("zhangsan");
        Person zhangsan2 = new Person("zhangsan");

        System.out.println(zhangsan == zhangsan2);//false
        System.out.println(zhangsan.equals(zhangsan2));//false

        Person2 zhangsan3 = new Person2("zhangsan");
        Person2 zhangsan4 = new Person2("zhangsan");

        System.out.println(zhangsan3 == zhangsan4);//false
        System.out.println(zhangsan3.equals(zhangsan4));//false


    }

}

class Person{

    String name;

    public Person(String name)
    {
        this.name = name;
    }


}

class Person2{

    String name;

    public Person2(String name)
    {
        this.name = name;
    }

    @Override
    public boolean equals(Object obj) {
        Person2 person2 = (Person2) obj;
        return  this.name.equals(person2.name);
    }
}
